package com.magenta.maxoptra.gis;

import com.magenta.maxoptra.dto.GeoLocationRecord;
import com.magenta.maxoptra.gis.commons.Pair;
import com.magenta.maxoptra.gis.commons.Quadruple;
import com.magenta.maxoptra.gis.commons.Triple;

import javax.naming.NamingException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/*
* Project: MaxOptRA v1.1
* User: Ivan Usalko
* Date: 30.01.2009 16:26:26
*
* Copyright (c) 1999-2009 Magenta Corporation Ltd. All Rights Reserved.
* Magenta Technology proprietary and confidential.
* Use is subject to license terms.
*/
/**
 * Interface PersistedCache ...
 * Persisted state of GeoInfoCache.
 */

public interface PersistedCache {
    public static final int DEFAULT_MAX_RECORDS_IN_LOCAL_CACHE = 10000;
    public static final int DEFAULT_MIN_RECORDS_BEFORE_FLUSH = 1000;
	public static final int MSECS_IN_HOUR = 60 * 60 * 1000;
    public static final long DEFAULT_MAX_DELAY_AFTER_LAST_FLUSH = 3 * MSECS_IN_HOUR;

    /**
     * Request route details in persistent state.
     * @param providerName name of external provider for routing info.
     * @param versionApi version API for access to external provider.
     * @param source from location.
     * @param destination to location. @return null if not find.
     */
    @Deprecated
    public Pair<Double, Long> findRoute(String providerName, String versionApi, GeographicCoordinates source, GeographicCoordinates destination);


    List<RouteRecord> findRoutes(Long cid, GeographicCoordinates source);

    /**
     * Try to persist local cache into DB,
     * then clear.
     * Always be synchronized.
     * @return true if success.
     */
    public boolean flushLocalCache() throws NamingException, SQLException, InterruptedException, IllegalAccessException;

    /**
     * Remove request from persisted state of GeoInfo Cache.
     * @param key id of request.
     * @return true if success.
     */
    public boolean removeRouteInfo(RouteRequestRecordPK key);


    /**
     * Remove path request from persisted state of GeoInfo Cache.
     * @param key id of request.
     * @return true if success.
     */
    boolean removeRoutePathInfo(RouteRequestRecordPK key);

    /**
     * Put pair key, value into persisted cache.
     * @param pk primary key {@link RouteRequestRecordPK}.
     * @param leg response info.
     */
    void persistRouteInfo(RouteRequestRecordPK pk, Quadruple<Double, Long, Long, Integer> leg) throws NamingException, SQLException, InterruptedException, IllegalAccessException;

    /**
     * Internal method for synchronization between two states in memory and persisted.
     * @param cid unique code @see Apis#cid
     * @param from from location.
     * @param to to location.
     * @return null if not find.
     */
	public Quadruple<Double, Long, Long, Integer> findRouteByCID(Long cid, GeographicCoordinates from, GeographicCoordinates to);

    /**
     * Put pair key, value into persisted cache.
     * @param pk primary key {@link RouteRequestRecordPK}.
     * @param path response info.
     */
    public void persistRoutePathInfo(RouteRequestRecordPK pk, List<GeoLocationRecord> path) throws NamingException, SQLException, InterruptedException, IllegalAccessException;


    /**
     * Internal method for synchronization between two states in memory and persisted.
     * @param cid unique code @see Apis#cid
     * @param source from location.
     * @param destination location.
     * @return null if not find.
     */
    public Triple<List<GeoLocationRecord>, Long, Date> findRoutePath(Long cid, GeographicCoordinates source, GeographicCoordinates destination);

    /**
     * Remove all cached entries before this date
     */
    void removeBeforeDate(long cid, long date);

    /**
     * Get date of first oldest entry of cache for specific CID
     */
    long getDateOfFirstOldest(long cid);
}